﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 海外机构目录
    /// </summary>
    public class HaiWaiJiGouMuLu
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder coordJsonBuilder = new StringBuilder("{");

            List<string> deptList = new List<string>();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string name = Convert.ToString(dataTable.Rows[index][0]).Trim();
                string coord = Convert.ToString(dataTable.Rows[index][3]).Trim();

                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(coord))
                    continue;

                string[] coordArr = coord.Split(new char[] { ',' });
                if (coordArr.Length == 2)
                {
                    if (coordJsonBuilder.Length == 1)
                    {
                        coordJsonBuilder.AppendFormat("\"{0}\":[{1},{2}]",
                            name,
                            Convert.ToDecimal(coordArr[0]),
                            Convert.ToDecimal(coordArr[1]));
                    }
                    else
                    {
                        coordJsonBuilder.AppendLine();
                        coordJsonBuilder.AppendFormat(",\"{0}\":[{1},{2}]",
                            name,
                            Convert.ToDecimal(coordArr[0]),
                            Convert.ToDecimal(coordArr[1]));
                    }
                }
            }
            coordJsonBuilder.Append("}");

            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{\"coordMap\":{0}}}", coordJsonBuilder);
            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath,
                builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}
